/**
 * 
 */
package image.bmp;

import image.RGBColor;
import image.Image;
import image.header.BMPHeader;
import image.header.DIBHeader;

/**
 * @author nquangtrung
 *
 */
public class BMPImage implements Image {
	public static final String TAG = "BMPImage";
	public static final int RGB_DEPTH = 3;
	public static final int ARGB_DEPTH = 4;
	public static final int GRAYSCALE_DEPTH = 1;
	
	protected RGBColor[][] rgbColorMap;
	protected BMPHeader bmpHeader;
	protected DIBHeader dibHeader;
	public BMPImage() {
		
	}

	public BMPImage(byte[] bColorMap, BMPHeader bmp, DIBHeader dib) {
		this.setBMPHeader(bmp);
		this.setDIBHeader(dib);
		
		int width = this.getWidth();
		int height = this.getHeight();
		int depth = this.getDepth();
		
		RGBColor[][] cColorMap = new RGBColor[width][height];
		int index = 0;
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				// default as RGB_DEPTH
				byte b = bColorMap[index];
				byte g = bColorMap[index + 1];
				byte r = bColorMap[index + 2];
				cColorMap[x][y] = new RGBColor(r, g, b);
				
				index += depth;
			}
			// remove padding
			if (width % 4 != 0)
				index += width % 4;
		}
		this.setColorMap(cColorMap);
	}

	public int getDepth() {
		return this.getDIBHeader().getDepth();
	}
	/* (non-Javadoc)
	 * @see image.Image#getRGB(int, int)
	 */
	@Override
	public int getRGB(int x, int y) {
		return x + y;
	}

	/* (non-Javadoc)
	 * @see image.Image#getRed(int, int)
	 */
	@Override
	public int getRed(int x, int y) {
		return this.getColorMap()[x][y].getRed();
	}

	/* (non-Javadoc)
	 * @see image.Image#getBlue(int, int)
	 */
	@Override
	public int getBlue(int x, int y) {
		return this.getColorMap()[x][y].getBlue();
	}

	/* (non-Javadoc)
	 * @see image.Image#getGreen(int, int)
	 */
	@Override
	public int getGreen(int x, int y) {
		return this.getColorMap()[x][y].getGreen();
	}

	@Override
	public int getWidth() {
		return this.getDIBHeader().getWidth();
	}

	@Override
	public int getHeight() {
		// TODO Auto-generated method stub
		return this.getDIBHeader().getHeight();
	}

	/**
	 * @return the bmpHeader
	 */
	public BMPHeader getBMPHeader() {
		return bmpHeader;
	}

	/**
	 * @param bmpHeader the bmpHeader to set
	 */
	public void setBMPHeader(BMPHeader bmpHeader) {
		this.bmpHeader = bmpHeader;
	}

	/**
	 * @return the dibHeader
	 */
	public DIBHeader getDIBHeader() {
		return dibHeader;
	}

	/**
	 * @param dibHeader the dibHeader to set
	 */
	public void setDIBHeader(DIBHeader dibHeader) {
		this.dibHeader = dibHeader;
	}
	/**
	 * @return the rgbColorMap
	 */
	public RGBColor[][] getColorMap() {
		return rgbColorMap;
	}
	/**
	 * @param rgbColorMap the rgbColorMap to set
	 */
	public void setColorMap(RGBColor[][] colorMap) {
		this.rgbColorMap = colorMap;
	}
	
	public void setRGBColor(int x, int y, RGBColor color) {
		this.getColorMap()[x][y] = color;
	}

}
